import Vue from 'vue'
import VueRouter from 'vue-router'
import My from '../views/My'
import errorInfo from '../views/errorInfo'
import classDetail from '../components/classDetail'
import timetableDetail from '../components/timetableDetail'
import personalSetting from '../views/personalSetting'
import settingAccountBinding from '../components/settingAccountBinding'
import settingPersonalInformation from '../components/settingPersonalInformation'
import Home from '../views/Home.vue'
import Course from '../views/Course.vue'
import details from '../views/details.vue'
import GoldPosition from '../views/GoldPosition.vue'
import GoldClass from '../views/GoldClass.vue'
import ZeroBasis from '../components/ZeroBasis.vue'
import Java2020 from '../views/Java2020.vue'
import shop from '../views/Shop.vue'
import pyp from '../components/pyp.vue'
import Combat from '../views/Combat.vue'
import CombatDetail from '../components/CombatDetail.vue'



Vue.use(VueRouter)


const routes = [

  { //主页
    path:'/',
    name:'Home',
    component:Home,
    meta:{
      title:'主页'
    }
  },
  {//购物车
    path: '/shop',
    name: 'shop',
    component: shop,
    meta:{
      title:'购物车'
    }
  },
  {//订单
    path: '/pyp',
    name: 'pyp',
    component: pyp,
    meta:{
      title:'订单'
    }
  },


  
  { //404
    path: '/errorInfo',
    name: 'errorInfo',
    component: errorInfo,
    meta:{
      title:'404'
    }
  },
  { //免费课程
    path: '/Course',
    name: 'Course',
    component: Course,
    meta:{
      title:'免费课程'
    }
  },
  { //免费课程下的详情页
    path: '/details/:id',
    name: 'details',
    component: details
  },
  { //实战课程
    path:'/Combat',
    name:'Combat',
    component:Combat,
    meta:{
      title:'实战课程'
    }
  },
  { //实战课程下的详情
    path:'/CombatDetail/:id',
    name:'CombatDetail',
    component:CombatDetail
  },

  // 金职位
  {
    path: '/GoldPosition',
    name: 'GoldPosition',
    component: GoldPosition,
    meta:{
      title:'金职位'
    }
  },
  { //金职位下的详情
    path: '/GoldClass',
    name: 'GoldClass',
    component: GoldClass
  },
  
  {
    path: '/Java2020',
    name: 'Java2020',
    component: Java2020
  },
  {
    path: '/ZeroBasis',
    name: 'ZeroBasis',
    component: ZeroBasis
  },
  
  { //个人设置
    path: '/personalSetting',
    name: 'personalSetting',
    // redirect: '/personalSetting/settingAccountBinding:idx',
    component: personalSetting,
    meta:{
      title:'个人设置'
    },
    children: [
      {
        path: '/',
        name: 'settingAccountBinding',
        component: settingAccountBinding
      },
      {
        path: 'settingPersonalInformation/:idx',
        name: 'settingPersonalInformation',
        component: settingPersonalInformation
      },
    ]
  },
  { //个人中心
    path: '/My',
    name: 'My',
    component: My,
    meta:{
      title:'个人中心'
    },
    children: [
      {
        path: '/',
        name: 'classDetail',
        component: classDetail
      },
      {
        path: 'timetableDetail/:idx',
        name: 'timetableDetail',
        component: timetableDetail
      }
    ]
  },
	{//实战主页
		path:'/Combat',
		name:'Combat',
		component:Combat
	},
	{//实战主页
		path:'/CombatDetail',
		name:'CombatDetail',
		component:CombatDetail
	}
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})






router.beforeEach((to, from, next) => {

  //获取token
  let userInfo = JSON.parse(localStorage.getItem('data'))
    if(userInfo === null){
        userInfo = {authToken:''}
    }
  
    //必须要登录才能访问的页面
    let access = to.path==='/My' ||to.path === '/shop' || to.path === '/pyp' || to.path === '/personalSetting'

  // console.log(to.matched)
  // console.log('to:',to.name)
  // console.log('from:',from.name)

  //如果是未匹配到的路由，引导到 404 页面
  if (to.matched.length === 0) { 
    // console.log('to:',to.name)
    // console.log('from:',from.name)
    from.name ? next({
      name: from.name
    }) : next('/errorInfo'); 
  } 

  else if(to.path === '/'){
    next()
  }

  //登录判断
  else if(access){
    if(userInfo.authToken === ''){
      Vue.prototype.$message({
        message:'请先登录',
        type:"error"
      })
      next('/')
    }else{
      next()
    }
  }
  
  
  
  else {
    next(); //如果匹配到正确跳转
  }

})

//消除重复点击问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
 return originalPush.call(this, location).catch(err => err)
}


export default router
